Skip to content

Fix TaskInstance crash with non-serialized operators missing get_weight#64557

Merged
vatsrahul1001 merged 2 commits intoapache:mainfrom
astronomer:fix/weight-rule-non-serialized-operator
Apr 1, 2026
Merged

Fix TaskInstance crash with non-serialized operators missing get_weight#64557
vatsrahul1001 merged 2 commits intoapache:mainfrom
astronomer:fix/weight-rule-non-serialized-operator

Conversation

@kaxil
Copy link
Copy Markdown
Member

@kaxil kaxil commented Mar 31, 2026

  • TaskInstance.refresh_from_task() and insert_mapping() call task.weight_rule.get_weight() unconditionally, but BaseOperator (task-sdk) stores weight_rule as a WeightRule enum -- a plain string with no get_weight method
  • Only SerializedBaseOperator has a @property that lazily converts the enum to a PriorityWeightStrategy (which has get_weight)
  • This breaks any code path that creates a TaskInstance from a non-serialized operator (tests, external tools, plugins)
  • Production scheduler is unaffected since it always uses serialized DAGs

Fix

Before calling get_weight(), check whether the weight_rule object supports it. If not, convert it via validate_and_load_priority_weight_strategy() -- the same function SerializedBaseOperator uses. Uses hasattr rather than isinstance so duck-typed custom strategies (WeightRuleProtocol) pass through without double-wrapping.

Gotchas

  • The conversion only triggers for non-serialized operators (the hasattr check is a no-op for serialized ones that already have get_weight)
  • WeightRuleProtocol from task-sdk/types.py defines get_weight structurally, so hasattr is the correct check over isinstance(PriorityWeightStrategy)

TaskInstance.refresh_from_task() and insert_mapping() call
task.weight_rule.get_weight() unconditionally, but BaseOperator
stores weight_rule as a WeightRule enum (no get_weight method).
Only SerializedBaseOperator's @Property converts it to a
PriorityWeightStrategy. This breaks any code creating a
TaskInstance from a non-serialized operator.

Convert weight_rule via validate_and_load_priority_weight_strategy()
when it lacks get_weight, preserving duck-typed custom strategies.
@kaxil kaxil requested review from XD-DENG and ashb as code owners March 31, 2026 21:47
@kaxil kaxil added this to the Airflow 3.2.1 milestone Mar 31, 2026
@kaxil kaxil requested a review from uranusjr March 31, 2026 21:56
@kaxil kaxil requested a review from vatsrahul1001 April 1, 2026 15:17
@kaxil kaxil modified the milestones: Airflow 3.2.1, Airflow 3.2.0 Apr 1, 2026
@vatsrahul1001 vatsrahul1001 added the backport-to-v3-2-test Mark PR with this label to backport to v3-2-test branch label Apr 1, 2026
@vatsrahul1001 vatsrahul1001 merged commit 860277d into apache:main Apr 1, 2026
82 checks passed
@vatsrahul1001 vatsrahul1001 deleted the fix/weight-rule-non-serialized-operator branch April 1, 2026 17:11
@github-actions
Copy link
Copy Markdown

github-actions bot commented Apr 1, 2026

Backport failed to create: v3-2-test. View the failure log Run details

Note: As of Merging PRs targeted for Airflow 3.X
the committer who merges the PR is responsible for backporting the PRs that are bug fixes (generally speaking) to the maintenance branches.

In matter of doubt please ask in #release-management Slack channel.

Status Branch Result
v3-2-test Commit Link

You can attempt to backport this manually by running:

cherry_picker 860277d v3-2-test

This should apply the commit to the v3-2-test branch and leave the commit in conflict state marking
the files that need manual conflict resolution.

After you have resolved the conflicts, you can continue the backport process by running:

cherry_picker --continue

If you don't have cherry-picker installed, see the installation guide.

vatsrahul1001 pushed a commit that referenced this pull request Apr 1, 2026
…ht (#64557)

* Fix TaskInstance crash with non-serialized operators missing get_weight

(cherry picked from commit 860277d)
@vatsrahul1001
Copy link
Copy Markdown
Contributor

Manual bacport #64597

kaxil added a commit that referenced this pull request Apr 1, 2026
…ht (#64557) (#64597)

(cherry picked from commit 860277d)

Co-authored-by: Kaxil Naik <kaxilnaik@gmail.com>
Subham-KRLX pushed a commit to Subham-KRLX/airflow that referenced this pull request Apr 3, 2026
…ht (apache#64557)

* Fix TaskInstance crash with non-serialized operators missing get_weight
Suraj-kumar00 pushed a commit to Suraj-kumar00/airflow that referenced this pull request Apr 7, 2026
…ht (apache#64557)

* Fix TaskInstance crash with non-serialized operators missing get_weight
vatsrahul1001 added a commit that referenced this pull request Apr 8, 2026
…ht (#64557) (#64597)

(cherry picked from commit 860277d)

Co-authored-by: Kaxil Naik <kaxilnaik@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport-to-v3-2-test Mark PR with this label to backport to v3-2-test branch

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants